package com.tyg.service.impl;

import com.tyg.entity.IsVip;
import com.tyg.entity.UserInfo;
import com.tyg.mapper.IsVipMapper;
import com.tyg.service.IsVipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tyg.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 顾客表 服务实现类
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@Service
@Transactional
public class IsVipServiceImpl extends ServiceImpl<IsVipMapper, IsVip> implements IsVipService {

    @Autowired
    IsVipMapper isVipMapper;

    @Autowired
    UserInfoService userInfoService;

    @Override
    public IsVip selectAll(Integer workId) {
        return  isVipMapper.selectAll(workId);
    }

    @Override
    public IsVip selectByWorkId(Integer workId) {
        IsVip isVip=isVipMapper.selectWorkId(workId);
        return  isVip;
    }

    @Override
    public void insertNew(Integer workId) {
        IsVip isVip=new IsVip();
        UserInfo userInfo=userInfoService.selectById(workId);
        isVip.setWorkId(workId);
        isVip.setRoleId(userInfo.getRoleId());
        isVip.setIsVip(1);
        isVip.setMoney(0.00);
        isVipMapper.insert(isVip);
    }

    @Override
    public IsVip Invest(double totalAmt, Integer workId) {
        IsVip isVip=isVipMapper.selectWorkId(workId);
        double num=isVip.getMoney()+totalAmt;
        int vip=1;
        int roleId=8;
        if(totalAmt>=100){
            vip=2;
            roleId=9;
        }else if(num>=500){
            vip=2;
            roleId=9;
        }
        UserInfo user=userInfoService.selectById(workId);
        user.setRoleId(roleId);
        userInfoService.updateById(user);
        int ifVip=vip;
        int ifRoleId=roleId;
        Map map=new HashMap<>();
        map.put("num",num);
        map.put("workId",workId);
        map.put("ifVip",ifVip);
        map.put("ifRoleId",ifRoleId);
        int upd=isVipMapper.updateMoney(map);
        IsVip isVipNew=isVipMapper.selectWorkId(workId);
        return isVipNew;
    }

    @Override
    public void updateMoney(double totalAmt, Integer workId) {
        IsVip isVip=isVipMapper.selectWorkId(workId);
        double num=isVip.getMoney()+totalAmt;
        Map map=new HashMap<>();
        map.put("num",num);
        map.put("workId",workId);
        isVipMapper.updateMoney(map);
    }
}
